﻿using OMG.Fill.Vegetable.Model.Ent;
using OMG.Fill.Vegetable.Service;
using ProjectShare;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
namespace OMG.Fill.Vegetable.Model.UI.Controllers
{
    public class SetListController : ApiController
    {
        SetListServices setListServices = new SetListServices();
        
        //[ApiActionAuthorize("获取填报项目设置T_Set_List分页列表对象操作")]
        //[ApiActionException]
        public Pager<List<SetListModel>> PostPageList(SetListQuery query)
        {
            return setListServices.GetPageList(query);
        }
        //[ApiActionAuthorize("新增填报项目设置T_Set_List指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Post(SetListEntity model) {
            return setListServices.AddInfo(model);
        }
        //[ApiActionAuthorize("修改填报项目设置T_Set_List指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Put(SetListEntity model)
        {
            return setListServices.UpdateInfo(model);
        }
        //[ApiActionAuthorize("删除填报项目设置T_Set_List指定对象操作")]
        //[ApiActionException]
        public ResResult<int> Delete(string id)
        {
            return setListServices.DeleteInfo(id);
        }
        //[ApiActionAuthorize("获取填报项目设置T_Set_List指定对象操作")]
        //[ApiActionException]
        public ResResult<SetListEntity> Get(string id)
        {
            return setListServices.GetModel(id);
        }
        public ResResult<List<SortSetItem>> GetSearchList(string tableCode)
        {
            return setListServices.GetSearchList(tableCode);
        }
        public ResResult<List<SetListEntity>> getSetList(string TableCode)
        {
            return setListServices.getListAll(TableCode);
        }
        public ResResult<List<SetListEntity>> GetTabpaneNameList(string TableCode)
        {
            return setListServices.GetSortList(TableCode);
        }
        public async Task<HttpResponseMessage> PostExportExcel()
        {
            // Check whether the POST operation is MultiPart?  
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }

            // Prepare CustomMultipartFormDataStreamProvider in which our multipart form  
            // data will be loaded.  
            string fileSaveLocation = HttpContext.Current.Server.MapPath("~/Upload");
            CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
            List<string> files = new List<string>();

            try
            {
                // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.  
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (MultipartFileData file in provider.FileData)
                {
                    setListServices.ExportExcel(file.LocalFileName);
                    files.Add(Path.GetFileName(file.LocalFileName));
                }

                // Send OK Response along with saved file names to the client.  
                return Request.CreateResponse(HttpStatusCode.OK, files);
            }
            catch (System.Exception e)
            {
                return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
            }
        }
        public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
        {
            public CustomMultipartFormDataStreamProvider(string path) : base(path) { }

            public override string GetLocalFileName(HttpContentHeaders headers)
            {
                return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
            }
        }
    }
}